//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "NonLinearLeastSquare.h"
using namespace Cephei::QL::Math::Optimization;
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Math/Optimization/LeastSquareProblem.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CNonLinearLeastSquare::CNonLinearLeastSquare (Cephei::QL::Math::Optimization::IConstraint^ c, Double accuracy, UInt64 maxiter, Cephei::QL::Math::Optimization::IOptimizationMethod^ om) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CConstraint^ _Cc;
    COptimizationMethod^ _Com;
    try
    {
#ifdef HANDLE
        _phNonLinearLeastSquare = NULL;
#endif
        _Cc = safe_cast<CConstraint^> (c);
        _Cc->Lock();
        QuantLib::Constraint& _c = static_cast<QuantLib::Constraint&> (_Cc->GetReference ()); 
        QuantLib::Real _accuracy = (QuantLib::Real)ValueHelper::Convert (accuracy);
        QuantLib::Size _maxiter = (QuantLib::Size)ValueHelper::Convert (maxiter);
        _Com = safe_cast<COptimizationMethod^> (om);
        _Com->Lock();
        boost::shared_ptr<QuantLib::OptimizationMethod> _om = static_cast<boost::shared_ptr<QuantLib::OptimizationMethod>> (_Com->GetShared ()); 
        _ppNonLinearLeastSquare = new boost::shared_ptr<QuantLib::NonLinearLeastSquare> (new QuantLib::NonLinearLeastSquare ( _c,  _accuracy,  _maxiter,  _om ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cc != nullptr) _Cc->Unlock();
        if (_Com != nullptr) _Com->Unlock();
    }
}
Cephei::QL::Math::Optimization::CNonLinearLeastSquare::CNonLinearLeastSquare (Cephei::QL::Math::Optimization::IConstraint^ c, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxiter) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CConstraint^ _Cc;
    try
    {
#ifdef HANDLE
        _phNonLinearLeastSquare = NULL;
#endif
        _Cc = safe_cast<CConstraint^> (c);
        _Cc->Lock();
        QuantLib::Constraint& _c = static_cast<QuantLib::Constraint&> (_Cc->GetReference ()); 
        QuantLib::Real _accuracy = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (accuracy) ? (QuantLib::Real)ValueHelper::Convert (accuracy->Value) : 1e-4); //4
        QuantLib::Size _maxiter = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (maxiter) ? (QuantLib::Size)ValueHelper::Convert (maxiter->Value) : 100); //4
        _ppNonLinearLeastSquare = new boost::shared_ptr<QuantLib::NonLinearLeastSquare> (new QuantLib::NonLinearLeastSquare ( _c,  _accuracy,  _maxiter ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cc != nullptr) _Cc->Unlock();
    }
}
Cephei::QL::Math::Optimization::CNonLinearLeastSquare::CNonLinearLeastSquare (boost::shared_ptr<QuantLib::NonLinearLeastSquare>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phNonLinearLeastSquare = NULL;
#endif
	_ppNonLinearLeastSquare = &childNative;
    
}
Cephei::QL::Math::Optimization::CNonLinearLeastSquare::CNonLinearLeastSquare (QuantLib::NonLinearLeastSquare& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phNonLinearLeastSquare = NULL;
#endif
	_ppNonLinearLeastSquare = new boost::shared_ptr<QuantLib::NonLinearLeastSquare> (&childNative);
    
    _NonLinearLeastSquareOwner = owner;
    
}

Cephei::QL::Math::Optimization::CNonLinearLeastSquare::CNonLinearLeastSquare (CNonLinearLeastSquare^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phNonLinearLeastSquare = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppNonLinearLeastSquare = new boost::shared_ptr<QuantLib::NonLinearLeastSquare> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Optimization::CNonLinearLeastSquare::CNonLinearLeastSquare (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phNonLinearLeastSquare = NULL;
#endif
	if (!t->IsSubclassOf(CNonLinearLeastSquare::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CNonLinearLeastSquare::CNonLinearLeastSquare (QuantLib::Handle<QuantLib::NonLinearLeastSquare>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phNonLinearLeastSquare = &childNative;
	_ppNonLinearLeastSquare = &static_cast<boost::shared_ptr<QuantLib::NonLinearLeastSquare>>(childNative.currentLink());
    
    _NonLinearLeastSquareOwner = owner;
}
Cephei::QL::Math::Optimization::CNonLinearLeastSquare::CNonLinearLeastSquare (QuantLib::Handle<QuantLib::NonLinearLeastSquare> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phNonLinearLeastSquare = &childNative;
	_ppNonLinearLeastSquare = &static_cast<boost::shared_ptr<QuantLib::NonLinearLeastSquare>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CNonLinearLeastSquare::CNonLinearLeastSquare (QuantLib::NonLinearLeastSquare childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phNonLinearLeastSquare = NULL;
#endif
	_ppNonLinearLeastSquare = new boost::shared_ptr<QuantLib::NonLinearLeastSquare> (new QuantLib::NonLinearLeastSquare (childNative));
    
}
#endif

Cephei::QL::Math::Optimization::CNonLinearLeastSquare::~CNonLinearLeastSquare ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppNonLinearLeastSquare != NULL)
    {
	    delete _ppNonLinearLeastSquare;
        _ppNonLinearLeastSquare = NULL;
    }
}
Cephei::QL::Math::Optimization::CNonLinearLeastSquare::!CNonLinearLeastSquare ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppNonLinearLeastSquare != NULL)
    {
	    delete _ppNonLinearLeastSquare;
    }
}
QuantLib::NonLinearLeastSquare& Cephei::QL::Math::Optimization::CNonLinearLeastSquare::GetReference ()
{
    if (_ppNonLinearLeastSquare == NULL) throw gcnew NativeNullException ();
	return **_ppNonLinearLeastSquare;
}
boost::shared_ptr<QuantLib::NonLinearLeastSquare>& Cephei::QL::Math::Optimization::CNonLinearLeastSquare::GetShared ()
{
    if (_ppNonLinearLeastSquare == NULL) throw gcnew NativeNullException ();
	return *_ppNonLinearLeastSquare;
}
QuantLib::NonLinearLeastSquare* Cephei::QL::Math::Optimization::CNonLinearLeastSquare::GetPointer ()
{
    if (_ppNonLinearLeastSquare == NULL) throw gcnew NativeNullException ();
	return &**_ppNonLinearLeastSquare;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::NonLinearLeastSquare>& Cephei::QL::Math::Optimization::CNonLinearLeastSquare::GetHandle ()
{
	if (_phNonLinearLeastSquare == NULL)
	{
		_phNonLinearLeastSquare = new Handle<QuantLib::NonLinearLeastSquare> (*_ppNonLinearLeastSquare);
	}
	return *_phNonLinearLeastSquare;
}
#endif
bool Cephei::QL::Math::Optimization::CNonLinearLeastSquare::HasNative () 
{
	return (_ppNonLinearLeastSquare != NULL);
}

Int32 Cephei::QL::Math::Optimization::CNonLinearLeastSquare::ExitFlag::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Integer _rv = (QuantLib::Integer)(*_ppNonLinearLeastSquare)->exitFlag ( );   
        Int32 _nrv = (Int32)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Int32 Cephei::QL::Math::Optimization::CNonLinearLeastSquare::IterationsNumber::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Integer _rv = (QuantLib::Integer)(*_ppNonLinearLeastSquare)->iterationsNumber ( );   
        Int32 _nrv = (Int32)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Optimization::CNonLinearLeastSquare::LastValue::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppNonLinearLeastSquare)->lastValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::Optimization::CNonLinearLeastSquare::Perform (Cephei::QL::Math::Optimization::ILeastSquareProblem^ lsProblem)
{
    CLeastSquareProblem^ _ClsProblem;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _ClsProblem = safe_cast<CLeastSquareProblem^> (lsProblem);
        _ClsProblem->Lock();
        QuantLib::LeastSquareProblem& _lsProblem = static_cast<QuantLib::LeastSquareProblem&> (_ClsProblem->GetReference ()); 
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppNonLinearLeastSquare)->perform ( _lsProblem );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_ClsProblem != nullptr) _ClsProblem->Unlock();
    }
}
Double Cephei::QL::Math::Optimization::CNonLinearLeastSquare::ResidualNorm::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppNonLinearLeastSquare)->residualNorm ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::Optimization::CNonLinearLeastSquare::Results::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppNonLinearLeastSquare)->results ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Optimization::INonLinearLeastSquare^ Cephei::QL::Math::Optimization::CNonLinearLeastSquare::SetInitialValue (Cephei::QL::Math::IArray^ initialValue)
{
    CArray^ _CinitialValue;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CinitialValue = safe_cast<CArray^> (initialValue);
        _CinitialValue->Lock();
        QuantLib::Array& _initialValue = static_cast<QuantLib::Array&> (_CinitialValue->GetReference ()); 
    	(*_ppNonLinearLeastSquare)->setInitialValue ( _initialValue );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CinitialValue != nullptr) _CinitialValue->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Optimization::INonLinearLeastSquare^ Cephei::QL::Math::Optimization::CNonLinearLeastSquare_Factory::Create (Cephei::QL::Math::Optimization::IConstraint^ c, Double accuracy, UInt64 maxiter, Cephei::QL::Math::Optimization::IOptimizationMethod^ om)
{
    return gcnew CNonLinearLeastSquare ( c,  accuracy,  maxiter,  om);
}
Cephei::QL::Math::Optimization::INonLinearLeastSquare^ Cephei::QL::Math::Optimization::CNonLinearLeastSquare_Factory::Create (Cephei::QL::Math::Optimization::IConstraint^ c, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxiter)
{
    return gcnew CNonLinearLeastSquare ( c,  accuracy,  maxiter);
}
